package com.geeklei.media.ui.common;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

public class AnimatorFactory {

    private static final int mDurationTime = 250;
    public static class AnimatorInvalidateUpdateListener implements AnimatorUpdateListener {
        private View mView;

        public AnimatorInvalidateUpdateListener(View view) {
            this.mView = view;
        }

        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            if (this.mView != null) {
                this.mView.invalidate();
            }
        }
    }

    private static int getHeight(View view) {
        int height = view.getHeight();
        if (height == 0) {
            return view.getContext().getResources().getDisplayMetrics().heightPixels;
        }
        return height;
    }

    private static int getWidth(View view) {
        int width = view.getWidth();
        if (width == 0) {
            return view.getContext().getResources().getDisplayMetrics().widthPixels;
        }
        return width;
    }

    public static ValueAnimator animateInBottomView(View view) {
        ValueAnimator ofFloat = ObjectAnimator.ofFloat(view, "translationY", new float[]{(float) getHeight(view), 0.0f});
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.setDuration(mDurationTime);
        ofFloat.addUpdateListener(new AnimatorInvalidateUpdateListener(view));
        ofFloat.start();
        view.setVisibility(View.VISIBLE);
        return ofFloat;
    }

    public static ValueAnimator animateInTopView(View view) {
        ValueAnimator ofFloat = ObjectAnimator.ofFloat(view, "translationY", new float[]{(float) (-getHeight(view)), 0.0f});
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.setDuration(mDurationTime);
        ofFloat.addUpdateListener(new AnimatorInvalidateUpdateListener(view));
        ofFloat.start();
        view.setVisibility(View.VISIBLE);
        return ofFloat;
    }

    public static ValueAnimator animateInLeftView(View view) {
        ValueAnimator ofFloat = ObjectAnimator.ofFloat(view, "translationX", new float[]{(float) (-getWidth(view)), 0.0f});
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.setDuration(mDurationTime);
        ofFloat.addUpdateListener(new AnimatorInvalidateUpdateListener(view));
        ofFloat.start();
        view.setVisibility(View.VISIBLE);
        return ofFloat;
    }

    public static ValueAnimator animateInRightView(View view) {
        ValueAnimator ofFloat = ObjectAnimator.ofFloat(view, "translationX", new float[]{(float) getWidth(view), 0.0f});
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.setDuration(mDurationTime);
        ofFloat.addUpdateListener(new AnimatorInvalidateUpdateListener(view));
        view.setVisibility(View.VISIBLE);
        ofFloat.start();
        return ofFloat;
    }

    public static Animator animateOutBottomView(View view) {
        Animator ofFloat = ObjectAnimator.ofFloat(view, "translationY", new float[]{0.0f, (float) view.getHeight()});
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.setDuration(mDurationTime);
        ofFloat.addListener(new ViewGoneAnimatorListener(view));
        ofFloat.start();
        return ofFloat;
    }

    public static Animator animateOutTopView(View view) {
        Animator ofFloat = ObjectAnimator.ofFloat(view, "translationY", new float[]{0.0f, (float) (-view.getHeight())});
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.setDuration(mDurationTime);
        ofFloat.addListener(new ViewGoneAnimatorListener(view));
        ofFloat.start();
        return ofFloat;
    }

    public static Animator animateOutLeftView(View view) {
        Animator ofFloat = ObjectAnimator.ofFloat(view, "translationX", new float[]{0.0f, (float) (-getWidth(view))});
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.setDuration(mDurationTime);
        ofFloat.addListener(new ViewGoneAnimatorListener(view));
        ofFloat.start();
        return ofFloat;
    }

    public static Animator animateOutRightView(View view) {
        Animator ofFloat = ObjectAnimator.ofFloat(view, "translationX", new float[]{0.0f, (float) getWidth(view)});
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.setDuration(mDurationTime);
        ofFloat.addListener(new ViewGoneAnimatorListener(view));
        ofFloat.start();
        return ofFloat;
    }

    public static Animator animateAlphaIn(View view) {
//        Animator ofFloat = ObjectAnimator.ofFloat(view, MIUI.alpha, new float[]{0.0f, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT});
        Animator ofFloat = ObjectAnimator.ofFloat(view, "alpha", new float[]{0.0f,0.9f});
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.setDuration(mDurationTime);
        ofFloat.start();
        view.setVisibility(View.VISIBLE);
        return ofFloat;
    }

    public static Animator animateAlphaOut(View view) {
//        Animator ofFloat = ObjectAnimator.ofFloat(view, MIUI.alpha, new float[]{DefaultRetryPolicy.DEFAULT_BACKOFF_MULT, 0.0f});
        Animator ofFloat = ObjectAnimator.ofFloat(view,"alpha", new float[]{0.9f, 0.0f});
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.setDuration(mDurationTime);
        ofFloat.addListener(new ViewGoneAnimatorListener(view));
        ofFloat.start();
        return ofFloat;
    }
}
